package com.resys.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import lombok.Data;
import org.springframework.stereotype.Service;

import com.resys.entity.enums.PageSize;
import com.resys.entity.query.CustomerQuery;
import com.resys.entity.po.Customer;
import com.resys.entity.vo.PaginationResultVO;
import com.resys.entity.query.SimplePage;
import com.resys.mappers.CustomerMapper;
import com.resys.service.CustomerService;
import com.resys.utils.StringTools;
import org.springframework.util.DigestUtils;


/**
 *  业务接口实现
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

	@Resource
	private CustomerMapper<Customer, CustomerQuery> customerMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<Customer> findListByParam(CustomerQuery param) {
		return this.customerMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(CustomerQuery param) {
		return this.customerMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<Customer> findListByPage(CustomerQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<Customer> list = this.findListByParam(param);
		PaginationResultVO<Customer> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(Customer bean) {
		return this.customerMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<Customer> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.customerMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<Customer> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.customerMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(Customer bean, CustomerQuery param) {
		StringTools.checkParam(param);
		return this.customerMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(CustomerQuery param) {
		StringTools.checkParam(param);
		return this.customerMapper.deleteByParam(param);
	}

	/**
	 * 根据CustomerId获取对象
	 */
	@Override
	public Customer getCustomerByCustomerId(String customerId) {
		return this.customerMapper.selectByCustomerId(customerId);
	}

	/**
	 * 根据CustomerId修改
	 */
	@Override
	public Integer updateCustomerByCustomerId(Customer bean, String customerId) {
		return this.customerMapper.updateByCustomerId(bean, customerId);
	}

	/**
	 * 根据CustomerId删除
	 */
	@Override
	public Integer deleteCustomerByCustomerId(String customerId) {
		return this.customerMapper.deleteByCustomerId(customerId);
	}

	/**
	 * 根据ClientName获取对象
	 */
	@Override
	public Customer getCustomerByClientName(String clientName) {
		return this.customerMapper.selectByClientName(clientName);
	}

	/**
	 * 根据ClientName修改
	 */
	@Override
	public Integer updateCustomerByClientName(Customer bean, String clientName) {
		return this.customerMapper.updateByClientName(bean, clientName);
	}

	/**
	 * 根据ClientName删除
	 */
	@Override
	public Integer deleteCustomerByClientName(String clientName) {
		return this.customerMapper.deleteByClientName(clientName);
	}

	@Override
	public void register(String company,String phone, String userName, String registerPassword, String realName) {
		Customer customer = customerMapper.selectByClientName(userName);
		if (customer != null) {
			throw new RuntimeException("用户名已存在");
		}
		customer = customerMapper.selectByPhone(phone);
		if (customer != null) {
			throw new RuntimeException("手机号已存在");
		}
		customer = new Customer();
        customer.setPhone(phone);
        customer.setClientName(userName);
        customer.setPassword(DigestUtils.md5DigestAsHex(registerPassword.getBytes()));
        customer.setRealname(realName);
		customer.setCreateAt(new Date());
		customer.setCustomerId(StringTools.getRandomString(10));
		customer.setAccountStatus(1);
		customer.setCompany(company);
		customerMapper.insert(customer);

	}

	public Integer getCountLogin(LocalDateTime start, LocalDateTime end) {
		Integer count = 0;
		if((customerMapper.getCountLogin(start, end))==null){
			return count;
		}
		return customerMapper.getCountLogin(start, end);
	}
}